﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Serialization;

namespace Com_Orion_Test_DLL
{
    public class xmlCtrl
    {    
        private string fileName = System.IO.Directory.GetCurrentDirectory() + "\\setting.xml";
        private XMLINFO tmpXMLINFO;
        private TestProgramInfo[] arrayTP;
        private LogInfo[] arrayLog;
        /// <summary>
        /// コンストラクタ
        /// </summary>
        public xmlCtrl()
        {
            CommInit();
        }
        /// <summary>
        /// xmlCtrlのオーバーロード
        /// 既存のファイル名(setting.xml)以外を利用するときに用いる
        /// </summary>
        /// <param name="filename">読み込みxmlファイル名</param>
        public xmlCtrl(string filename)
        {
            if (File.Exists(filename))
            {
                this.fileName = filename;
                CommInit();

            }
        }
        

        public TestProgramInfo[] ArrayTP
        {
            get { return this.arrayTP; }
        }

        public LogInfo[] ArrayLog
        {
            get { return this.arrayLog; }
        }
        
        public void FileWrite()
        {
            if (!String.IsNullOrEmpty(this.fileName))
            {
                using (FileStream fs1 = new System.IO.FileStream(@System.IO.Directory.GetCurrentDirectory() + "\\setting.xml", System.IO.FileMode.Create)){
                    XmlSerializer writeXml = new XmlSerializer(typeof(XMLINFO));
                    writeXml.Serialize(fs1, tmpXMLINFO);
                    fs1.Close();
                }
            }
        }
        /// <summary>
        /// コンストラクタ内初期化メソッド
        /// </summary>
        private void CommInit()
        {
            if (File.Exists(fileName))
            {
                using (FileStream reader = new FileStream(fileName, FileMode.Open))
                {
                    XmlSerializer readxmlTP = new XmlSerializer(typeof(XMLINFO));
                    tmpXMLINFO = ((XMLINFO)readxmlTP.Deserialize(reader));
                    arrayLog = tmpXMLINFO.LG;
                    arrayTP = tmpXMLINFO.TestPG;
                }

            }
        }
        
    }
}
